<style>

</style>
<template>
  <el-dialog
      title="域配置"
      :visible.sync="dialogVisible"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      width="60%"
      :before-close="clearForm">

    <el-form
        :label-position="labelPosition"
        label-width="120px"
        :model="saveData"
        :rules="rules"
        ref="ruleForm"
    >
      <el-form-item label="选择域" prop="domainCode">
        <el-select
            :disabled="saveData.id && saveData.id !==''"
            filterable
            v-model="saveData.domainCode"
            placeholder="请选择域">
          <el-option
              v-for="item in domainOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="基础数据名字" prop="fieldName">
        <el-input :disabled="saveData.id && saveData.id !==''" v-model="saveData.fieldName"></el-input>
      </el-form-item>
      <el-form-item label="基础数据描述">
        <el-input v-model="saveData.desc"></el-input>
      </el-form-item>
      <el-form-item label="数据类型" prop="dataType">
        <el-select
            filterable
            v-model="saveData.dataType"
            placeholder="请选择">
          <el-option
              v-for="item in dataTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item v-if="saveData.dataType === 'METADATA'" label="选择元数据" prop="metadataUuid">
        <el-select
            filterable
            v-model="saveData.metadataUuid"
            placeholder="请选择">
          <el-option
              v-for="item in metadataOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <!--  metadataUuid    -->
      <el-form-item label="脚本类型" prop="scriptType">
        <el-select
            filterable
            v-model="saveData.scriptType"
            placeholder="请选择">
          <el-option
              v-for="item in scriptTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="脚本返回类型" prop="resultType">
        <el-select
            filterable
            v-model="saveData.resultType"
            placeholder="请选择">
          <el-option
              v-for="item in resultTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="脚本内容" prop="scriptContent">
        <el-input :rows="10" type="textarea" placeholder="请输入脚本" v-model="saveData.scriptContent"></el-input>
      </el-form-item>
      <el-form-item label="脚本测试参数">
        <el-input :rows="10" type="textarea" placeholder="请输入脚本测试参数(JSON格式)" v-model="scriptContentTest"></el-input>
      </el-form-item>
      <el-form-item v-if="debugResult" label="脚本测试结果">
        <div style="background-color: black;color:#FFD700;line-height: 20px!important;">
          <span>是否执行成功：{{ debugResult.success }}</span><br>
          <span>执行结果类型：{{ debugResult.reqResultType }}</span><br>
          <span>脚本类型：{{ debugResult.scriptType }}</span><br>
          <span v-if="debugResult.success"><span>实际执行结果：{{ debugResult.realResult }}</span><br></span>
          <span v-if="!debugResult.success">执行异常：{{ debugResult.exceptionMessage }}</span>
        </div>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
    <el-button @click="clearForm">取 消</el-button>
    <el-button type="warning" @click="debug">脚本测试</el-button>
    <el-button :disabled="!testPass" type="primary" @click="save">确 定</el-button>
  </span>
  </el-dialog>
</template>
<script>

import {baseDataDebug, remoteValidateFieldName, saveBaseData} from "../../../api/basedataApi";
import {loadDataTypeSelector, loadDomainMetadataSelector, loadDomainSelector, loadResultTypeSelector, loadScriptTypeSelector} from "../../../api/loadSelectorApi";

export default {
  name: "baseDataForm",
  filters: {},
  components: {},
  data() {
    const validateBaseDataName = async (rule, value, callback) => {
      if (this.saveData.id) {
        callback()
      }
      let ret = await remoteValidateFieldName(value, this.saveData.domainCode);
      if (ret.data.result) {
        callback()
      } else {
        callback(new Error("基础数据名字不能重复"))
      }
    };
    return {
      labelPosition: 'right',
      dialogVisible: false,
      scriptContentTest: '',
      testPass: false,
      debugResult: undefined,
      saveData: {
        domainCode: '',
        fieldName: '',
        desc: '',
        dataType: '',
        metadataUuid: '',
        scriptType: '',
        scriptContent: '',
        resultType: '',
      },
      domainOptions: [],
      dataTypeOptions: [],
      scriptTypeOptions: [],
      resultTypeOptions: [],
      metadataOptions: [],
      rules: {
        fieldName: [
          {required: true, message: '此项必填', trigger: 'blur'},
          {validator: validateBaseDataName, trigger: 'blur'}
        ],
        domainCode: [
          {required: true, message: '此项必填', trigger: 'blur'},
        ],
        dataType: [
          {required: true, message: '此项必填', trigger: 'change'},
        ],
        scriptType: [
          {required: true, message: '此项必填', trigger: 'change'},
        ],
        resultType: [
          {required: true, message: '此项必填', trigger: 'change'},
        ],
        metadataUuid: [
          {required: true, message: '此项必填', trigger: 'change'},
        ],
        scriptContent: [
          {required: true, message: '此项必填', trigger: 'blur'},
        ],
      }
    }
  },
  created() {
  },
  mounted() {
  },
  methods: {
    async load(data) {
      let vm = this
      vm.domainOptions = await loadDomainSelector()
      vm.dataTypeOptions = await loadDataTypeSelector()
      vm.scriptTypeOptions = await loadScriptTypeSelector()
      vm.resultTypeOptions = await loadResultTypeSelector()
      if (data) {
        vm.saveData = vm.$cloneObj(data)
      }
      vm.dialogVisible = true
    },
    save() {
      let vm = this
      this.$refs['ruleForm'].validate(async (valid) => {
        if (valid) {
          await vm.doSave(this.saveData)
        }
      })
    },
    async doSave(saveData) {
      let vm = this
      let ret = await saveBaseData(saveData);
      if (ret.ok) {
        vm.$message({type: 'success', message: '保存成功!'});
      } else {
        vm.$message({type: 'error', message: '保存失败：' + ret.message});
      }
      vm.clearForm()
      vm.$parent.query()
    },
    clearForm() {
      let vm = this
      try {
        vm.debugResult = undefined
        vm.testPass = false
        vm.scriptContentTest = ''
        vm.saveData = {
          domainCode: '',
          fieldName: '',
          desc: '',
          dataType: '',
          metadataUuid: '',
          scriptType: '',
          scriptContent: '',
          resultType: '',
        }
        vm.$refs['ruleForm'].resetFields()
      } catch (e) {
        console.log(e)
      }
      vm.dialogVisible = false
    },
    debug() {
      this.$refs['ruleForm'].validate(async (valid) => {
        if (valid) {
          if (!this.scriptContentTest || this.scriptContentTest.trim() === '') {
            this.$message({type: 'error', message: '测试json不能为空'});
            return
          }
          let ret = await baseDataDebug(this.saveData, this.scriptContentTest);
          if (ret.ok) {
            this.debugResult = ret.data.result
            this.testPass = this.debugResult.success
          } else {
            this.$message({type: 'error', message: '测试失败：' + ret.message});
          }
        }
      })
    }
  },
  watch: {
    "saveData.domainCode": {
      async handler(newVal, oldVal) {
        if (newVal) {
          this.metadataOptions = []
          this.metadataOptions = await loadDomainMetadataSelector(newVal)
          if (this.saveData.id === '') {
            this.saveData.metadataUuid = ''
          }
        }
      },
      immediate: true
    }
  }
}
</script>
